const { Kafka } = require('kafkajs')

const kafka = new Kafka({
    clientId: 'my-app',
    brokers: ['123.207.201.177:9092']  // 使用服务器的 IP 和端口
})

const producer = kafka.producer()
const consumer = kafka.consumer({ groupId: 'test-group' })

const topic = 'test_notice'
const run = async () => {
    // Producing
    await producer.connect()
    await producer.send({
        topic: topic,
        messages: [
            { value: 'Hello KafkaJS user!1111' },
        ],
    })

    // Consuming
    await consumer.connect()
    await consumer.subscribe({ topic: topic, fromBeginning: true })

    await consumer.run({
        eachMessage: async ({ topic, partition, message }) => {
            console.log({
                partition,
                offset: message.offset,
                value: message.value.toString(),
            })
        },
    })
}

run().catch(console.error)

